// program : Database Browser : myFrame2.java // Modified version of myFrame.java : has better handling to try-catch block to report // Exception (java.sql.SQLException). One try-catch block in the method actionPerformed() // of the Class myActionListener accounts for all the methods which need to be caught for // Exception. But those methods need to throw Exception. // The name of DSN used in this program is 'MyDSN'. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.net.URL; public class myFrame2 extends JFrame { // Spaces added infront and rear to adjust the alignment JLabel labelHeader = new JLabel( " My Database Browser",JLabel.CENTER ); JLabel labelName = new JLabel( " Name " ); JLabel labelAddress = new JLabel( " Address " ); JLabel labelCity = new JLabel( " City " ); JLabel labelState = new JLabel( " State " ); JLabel labelZip = new JLabel( " Zip " ); JButton buttonPrev = new JButton( "Previous" ); JButton buttonReset = new JButton( " Reset " ); JButton buttonNext = new JButton( " Next " ); JButton buttonClose = new JButton( " Close " ); JTextField textFieldName = new JTextField(); JTextField textFieldAddress = new JTextField(); JTextField textFieldCity = new JTextField(); JTextField textFieldState = new JTextField(); JTextField textFieldZip = new JTextField(); // Necessary for JDBC-ODBC Connection Connection conn; Statement stmt; ResultSet rs; public myFrame2(String title) { super( title ); setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); myFrameInit(); // Event to Close the JFrame (this application) when 'x is clicked. addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing( java.awt.event.WindowEvent evt) { shutDown(); } } ); // Set up bttn to send an "action event" to this JFrame when the user clicks the button. buttonPrev.addActionListener( new myActionListener() ); buttonReset.addActionListener( new myActionListener() ); buttonNext.addActionListener( new myActionListener() ); buttonClose.addActionListener( new myActionListener() ); //Let's get the database running now! try { //Create a URL specifying ODBC data source name //Load the jdbc-odbc bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Connect to the URL conn = DriverManager.getConnection("jdbc:odbc:MyDSN", "", ""); //Load the first records LoadFirstRecord(); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } public void myFrameInit() { JPanel cpBig = new JPanel(); //inside this Panel other Panels reside JPanel cpLeft = new JPanel(); JPanel cpCenter = new JPanel(); JPanel cpRight = new JPanel(); // This panel is just created to create some margin JPanel cpBottom = new JPanel(); JPanel cpTop = new JPanel(); // Cannot add components directly to JFrame, so either need to add components to the JFrame's // content pane, or need to provide a new content pane. A content pane is a 'Container' that // contains all of the frame's visible components except for the menu bar (if there is one). getContentPane().add( cpBig ); // Creates a border with a raised beveled edge, using brighter shades of the component's // current background color for highlighting, and darker shading for shadows. // (In a raised border, highlights are on top and shadows are underneath.):[from jdk1.3.1 API Lib] cpBig.setBorder( BorderFactory.createRaisedBevelBorder() ); // BorderLayout: cpBig.setLayout( new BorderLayout() ); // Now add the rest of the Jpanels to the cpBig Jpanels cpBig.add( cpLeft, BorderLayout.WEST ); cpBig.add( cpCenter, BorderLayout.CENTER ); cpBig.add( cpRight, BorderLayout.EAST ); cpBig.add( cpBottom, BorderLayout.SOUTH ); cpBig.add( cpTop, BorderLayout.NORTH ); // cpLeft Pane: 5 Labels in one Column cpLeft.setLayout( new GridLayout(5,1,50,20) ); // cpCenter Pane: 5 TextFields in one Column cpCenter.setLayout( new GridLayout(5,1,50,20) ); // cpTop Pane: Label for the Frame, i.e. "My Database Browser" cpTop.setLayout( new FlowLayout(FlowLayout.CENTER) ); // cpBottom Pane: 4 Buttons in a row cpBottom.setLayout( new FlowLayout(FlowLayout.RIGHT,10,10) ); labelHeader.setFont( new Font("TimesRoman",Font.BOLD,24 ) ); cpTop.setBackground( new Color( 250,250,250 ) ); cpTop.add( labelHeader ); //labelHeader.setBounds( 40,10,300,50 ); cpBottom.setBackground( new Color( 250,250,250 ) ); cpBottom.add( buttonPrev ); cpBottom.add( buttonReset ); cpBottom.add( buttonNext ); cpBottom.add( buttonClose ); // Shortcut keys: ALT+p = buttonPrev, etc. buttonPrev.setMnemonic( 'p' ); buttonReset.setMnemonic( 'r' ); buttonNext.setMnemonic( 'n' ); buttonClose.setMnemonic( 'c' ); cpLeft.setBackground( new Color( 250,250,250 ) ); cpLeft.add( labelName ); cpLeft.add( labelAddress ); cpLeft.add( labelCity ); cpLeft.add( labelState ); cpLeft.add( labelZip ); cpRight.setBackground( new Color( 250,250,250 ) ); cpCenter.setBackground( new Color( 250,250,250 ) ); cpCenter.add( textFieldName ); cpCenter.add( textFieldAddress ); cpCenter.add( textFieldCity ); cpCenter.add( textFieldState ); cpCenter.add( textFieldZip ); } class myActionListener implements ActionListener { public void actionPerformed(ActionEvent evt) { // All the methods that throws exception have been included in // the following try-catch block, so no need to decare this block // else where, but those methods need to throw Exception. try { if( evt.getSource()==buttonPrev ) { GetPreviosRecord(); } else if( evt.getSource()==buttonReset ) { ResetRecord(); } else if( evt.getSource()==buttonNext ) { GetNextRecord(); } else if( evt.getSource()==buttonClose ) { System.out.println( "On buttonClose" ); stmt.close(); conn.close(); shutDown(); } } catch( java.lang.Exception ex ) { ex.printStackTrace(); } } } public void shutDown() { int returnVal=JOptionPane.showConfirmDialog( this, "Are you sure you want to quit?" ); if( returnVal==JOptionPane.YES_OPTION ) { // close out stuff System.exit(0); } } public void GetPreviosRecord() throws Exception { if( rs.previous() ) { updateTextFields(); } else { rs.first(); JOptionPane.showMessageDialog( this,"Already at the First Record!","Alert",JOptionPane.INFORMATION_MESSAGE ); } } public void ResetRecord() throws Exception { System.out.println( "On buttonReset" ); stmt.close(); LoadFirstRecord(); } public void GetNextRecord() throws Exception { if( rs.next() ) { updateTextFields(); } else { rs.last(); JOptionPane.showMessageDialog( this,"Already at the End of the Record!","Alert",JOptionPane.INFORMATION_MESSAGE ); } } public void updateTextFields() throws Exception { textFieldName.setText( rs.getString(2) + " " + rs.getString(3) ); textFieldAddress.setText( rs.getString(4) ); textFieldCity.setText( rs.getString(5) ); textFieldState.setText( rs.getString(6) ); textFieldZip.setText( rs.getString(7) ); } public void LoadFirstRecord() throws Exception { try { stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); rs = stmt.executeQuery( "SELECT * FROM address" ); if( rs.next() ) { updateTextFields(); } } catch( Exception ex ) { JOptionPane.showMessageDialog( this,"Could not connect (fatal)","Alert",JOptionPane.INFORMATION_MESSAGE ); System.exit(1); } } public static void main(String args[]) { /*try { UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" ); //UIManager.setLookAndFeel( "javax.swing.plaf.motif.MotifLookAndFeel" ); //UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ); } catch (Exception e) { }*/ myFrame2 m2 = new myFrame2( "Database Browser" ); m2.setSize( 400,350 ); m2.show(); } }